using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using BootstrapBlazor.Components;
using TrackSystem.IO.Logics;
using TrackSystem.IO.Loops;
using TrackSystem.IO.Models;

namespace TrackSystem.IO.Equip;

/// <summary>
/// 设备类
/// </summary>
public class EquipItem
{
    /// <summary>
    /// 通道名称
    /// </summary>
    [Display(Name = "名称")]
    public string? Name { get; set; }

    /// <summary>
    /// 通道描述
    /// </summary>
    [Display(Name = "描述")]
    [AutoGenerateColumn(Order = 20, Filterable = true, Searchable = true)]
    public string? Description { get; set; }
    
    /// <summary>
    /// 运行使能
    /// </summary>
    public bool Enabled { get; set; }

    /// <summary>
    /// 该设备下的所有部件
    /// </summary>
    public ConcurrentDictionary<string, EquipPartAttr>? Parts { get; set; }

    /// <summary>
    /// 添加传感器
    /// </summary>
    /// <param name="sensor"></param>
    /// <returns></returns>
    public bool TryAddSensor(ProtocolBase sensor)
    {
        Parts ??= new ConcurrentDictionary<string, EquipPartAttr>();

        var part = new EquipPartAttr()
        {
            Catogery = EquipPartCatogery.SENSOR,
            Name = sensor.Name,
            Description = "",
            Enabled = sensor.Enabled,
            Status = sensor.Fail_R > 0
        };
        return Parts.TryAdd(part.Name, part);
    }

    /// <summary>
    /// 添加传感器
    /// </summary>
    /// <param name="logic"></param>
    /// <returns></returns>
    public bool TryAddLogic(LogicItem logic)
    {
        if (Parts is null)
            Parts = new();

        var part = new EquipPartAttr()
        {
            Catogery = EquipPartCatogery.LOGIC,
            Name = logic.Name,
            Description = "",
            Enabled = logic.Enabled,
        };
        return Parts.TryAdd(part.Name, part);
    }

    /// <summary>
    /// 添加传感器
    /// </summary>
    /// <param name="loop"></param>
    /// <returns></returns>
    public bool TryAddLoop(LoopItem loop)
    {
        Parts ??= new ConcurrentDictionary<string, EquipPartAttr>();

        var part = new EquipPartAttr()
        {
            Catogery = EquipPartCatogery.LOOP,
            Name = loop.Name,
            Description = "",
            Enabled = loop.Enabled,
            Status = loop.Manual
        };
        return Parts.TryAdd(part.Name, part);
    }
}